package com.hotel.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotel.annotation.ControllerEndpoint;
import com.hotel.entity.Dish;
import com.hotel.response.Result;
import com.hotel.service.DishService;
import com.hotel.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 商品管理 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-05-30
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @ApiOperation(value = "根据分类查询商品")
    @GetMapping("/getDishList")
    public Result getDishList(@RequestParam(value = "id", required = false) String id,
                              @RequestParam(value = "level", required = false) Integer level,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                              @RequestParam(value = "size", required = false, defaultValue = "4") Integer size) {
        IPage<Dish> dishList = dishService.getDishList(id, level, name, status, current, size);
        return Result.ok().data("dishList", dishList);
    }

    @ApiOperation(value = "删除商品")
    @RequiresPermissions({"konwledge:delete"})
    @DeleteMapping("/deleteDishById")
    @ControllerEndpoint(exceptionMessage = "删除商品失败", operation = "删除商品")
    public Result deleteDishById(String id){
        int i = dishService.deleteDishById(id);
        return Result.ok().data("delete",i);
    }

    @ApiOperation("商品图上传")
    @PostMapping("/uploadDish")
    @ControllerEndpoint(exceptionMessage = "上传商品图失败", operation = "上传商品图")
    public Result uploadIcon(@RequestParam("file") MultipartFile file){
        String s = dishService.upload(file,"dish_image");
        return Result.ok().data("url",s);
    }

    @ApiOperation(value = "增加商品")
    @PostMapping("/addDish")
    @RequiresPermissions({"konwledge:add"})
    @ControllerEndpoint(exceptionMessage = "增加商品失败", operation = "增加商品")
    public Result addDish(@RequestBody DishVO dishVO){
        int i = dishService.addDish(dishVO);
        return Result.ok().data("addDish",i);
    }

    @ApiOperation(value = "修改商品")
    @RequiresPermissions({"konwledge:update"})
    @PutMapping("/updateDish")
    @ControllerEndpoint(exceptionMessage = "更新商品失败", operation = "更新商品")
    public Result updateDish(@RequestBody DishVO dishVO){
        int i = dishService.updateDish(dishVO);
        return Result.ok().data("updateDish",i);
    }

    @ApiOperation(value = "编辑商品信息")
    @GetMapping("/editDish")
    public Result editDish(@RequestParam("id") String id){
        Dish dish = dishService.editDish(id);
        return Result.ok().data("dish",dish);
    }

    @ApiOperation(value = "修改商品状态")
    @RequiresPermissions({"konwledge:status"})
    @PutMapping("/updateStatus")
    @ControllerEndpoint(exceptionMessage = "更新商品状态失败", operation = "更新商品状态")
    public Result updateStatus(@RequestParam("id") Long id,
                               @RequestParam("status") Integer status){
        int i = dishService.updateStatus(id, status);
        return Result.ok().data("updateStatus",i);
    }

}

